// pages/CNKIProject/DailyReport/AddForm.js\
var app = getApp();
var util = require('../../../utils/util.js');
var requestRoot = app.globalData.hostUrl;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    rules: [{
      name: 'workload',
      rules: {
        number: true,
        message: '请填写有效工作量'
      },
    }],
    projectlist: [
    ],
    ProjectIndex: [],
    projectSelect: '',

    worktype: [{ name: '正常', value: '1', checked: true }, { name: '加班', value: '2', checked: false }],
    worktypeSelect: '',

    formData: {},
    formList: {},
    ViewModule:[0],//添加和减少项目当前有多少个
    ViewModuleIndex:0//当前添加到第几个项目了
  },
  /**
   * 赋值项目Id
   */
  bindPickerChange: function (e) {
    let target=e.currentTarget.dataset;
    let index = e.detail.value;
    this.setData({
      [`ProjectIndex[${target.field}]`] : index,
      [`formList[${target.field}].projectId`]: this.data.projectlist[index].value
    })

  },
  /**
   * 赋值工作时长
   */
  workloadChange: function (e) {
    let target=e.currentTarget.dataset;
    this.setData({
      [`formList[${target.field}].workload`]: e.detail.value
    })
  },
  /**
   * 赋值工作类型
   */
  worktypeChange: function (e) {
    let target=e.currentTarget.dataset;
    this.setData({
      [`formList[${target.field}].worktype`]: e.detail.value
    })
  },
  /**
   * 赋值工作内容
   */
  jobContentChange: function (e) {
    let target=e.currentTarget.dataset;
    this.setData({
      [`formList[${target.field}].jobContent`]: e.detail.value
    })
  },
  /**
   * 上报时间
   */
  bindDateChange: function (e) {
    this.setData({
      [`formData.reportDate`]: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var _this=this;
    wx.cloud.callFunction({
      name: 'get-projects',
      data: {
        url: requestRoot+'/api/Values/ProjectInfos',
        WEBJSESSIONID:wx.getStorageSync('WEBJSESSIONID'),
        token:wx.getStorageSync('csrf-token')
      },
      success: function (res) {
        _this.setData({
           projectlist:JSON.parse(res.result),
           [`formList[0].projectId`]:JSON.parse(res.result)[0].value
         })
      }
    });
    _this.setData({//给予初始值
      [`formList[0].worktype`]:1
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      [`formData.creater`]: wx.getStorageSync('userName'),
      [`formData.createBy`]: wx.getStorageSync('userId'),
      [`formData.reportDate`]: util.formatTime(new Date(),"YY-MM-DD"),
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  /**
  * 添加项目
  */
  AddView:function(){
    this.data.ViewModuleIndex++;
    let ModuleConst=this.data.ViewModule.concat(this.data.ViewModuleIndex);
    this.setData({
      ViewModule:ModuleConst,
      [`formList[${this.data.ViewModuleIndex}].projectId`]: this.data.projectlist[0].value,
      [`formList[${this.data.ViewModuleIndex}].worktype`]:1
    })
  },
  /**
   * 减少项目
   */
  ReduceView:function()
  {
    if(this.data.ViewModuleIndex>0)
    {
      let module_temp=this.data.ViewModule;
      module_temp.pop();
      this.data.ViewModuleIndex--;
      this.setData({
          ViewModule:module_temp
      })
    }
  },
  submitForm: function () {
    var submitData = this.data.formData;
    let worktotal=0;
    this.data.formList.forEach(function(item,index){
      worktotal+=parseInt(item.workload);
    })
    if(worktotal>24)
    {
      wx.showToast({
        title: "一天24小时",
      })
    }
    submitData.workload =worktotal;

    submitData.list=JSON.stringify(this.data.formList);
    wx.showLoading({
      title: '提交中',
    })
    wx.cloud.callFunction({
      name: 'Daily-Submit',
      data: {
        submitData: submitData,
        url: requestRoot + "/API/Daily/add",
        token:wx.getStorageSync('csrf-token'),
        WEBJSESSIONID:wx.getStorageSync('WEBJSESSIONID')
      },
      success: function (e) {
          wx.hideLoading();
          var res=JSON.parse(e.result);
          if(res.flag==true)
          {
            wx.showToast({
              title: e.msg,
            })
          }else
          {
            wx.showToast({
              title: e.msg,
            })
          } 
       wx.navigateBack({//返回上一级
         delta: 1,
       })
      },fail:function(e)
      {
        wx.hideLoading();
        wx.showToast({
          title: e.msg,
        })
      }
      
    })
  }
})